package hard;

/**
 * 1793. 好子数组的最大分数
 * 地址：https://leetcode.cn/problems/maximum-score-of-a-good-subarray/description/?envType=daily-question&envId=2024-03-19
 */
public class 好子数组的最大分数_1793 {
    public static void main(String[] args) {
        int[] a = {1,4,3,7,4,5};
        System.out.println(maximumScore(a, 3));
    }

    /**
     * 双指针
     */
    public static int maximumScore(int[] nums, int k) {
        int left = k - 1, right = k + 1, n = nums.length;
        int res = 0;
        // 每次 nums[k]对应的值减1，若对应位置的元素要大于减1的数，可让下标移动
        for (int i = nums[k]; ; ) {
            while(left >= 0 && nums[left] >= i) {
                left--;
            }
            while(right < n && nums[right] >= i) {
                right++;
            }
            res = Math.max(res, i * (right - left - 1));
            if (left < 0 && right >= n) {
                break;
            }
            i = Math.max((left == -1 ? -1 : nums[left]), (right == n ? -1 : nums[right]));
            if (i == -1) {
                break;
            }

        }
        return res;
    }
}
